#include <gtk/gtk.h>   
#include <gst/gst.h>   
#include <signal.h>   
#include <string.h>   
#include <gst/interfaces/xoverlay.h>
#include <glade/glade.h>
#include <time.h>
#include <dirent.h>
#include <unistd.h>

#include "config.h"
#include "mp4_player.h"
#include "gif_player.h"
#include "jpg_player.h"
#include "socket_client.h"
#include "printer_local.h"
#include "message_handler.h"
#include "xml_client.h"
#include "sys_time.h"
#include "keyboard.h"
#include "sys_log.h"

static int sound_opening_flag = 0;

//show text
char *update_text(GtkWidget *view, char* text,int size) 
{
	gchar *markup;

	if(!text || !view) {
		SYS_LOG("show_text_in_new_win error");
		return NULL;
	}

	int str_len = strlen(text);
	markup=g_markup_printf_escaped("<span size=\"%d\" foreground=\"black\" >%s</span>",size,text);

	gtk_label_set_markup(GTK_LABEL(view),markup);
	
	gtk_widget_show(view);

	return NULL;
}
//show text end

static void save_config_win(GtkWidget* widget, gpointer data)
{
	printf("save_config_win\n");
    char* str = NULL;
    print_sys_config_s *sys_config = (print_sys_config_s *)data;
    str = gtk_entry_get_text(GTK_LABEL(sys_config->config_entry_url));
    if(str) {
        g_stpcpy(sys_config->sys_config.heart_beat_url,str);
       	save_url_config_file(CLIENT_SYS_HEART_BEAT,&(sys_config->sys_config));
		sprintf(sys_config->sys_config.device_id,"%s",g_strrstr(str,"printer"));
		update_text(sys_config->device_id_text,sys_config->sys_config.device_id,10000);
		save_text_config_file(CLIENT_SYS_CONFIG_FILE,&(sys_config->sys_config));
    }
   	gtk_widget_hide(sys_config->config_win);
    stop_soft_keyboard();
}

void show_config_win(GtkWidget* widget, gpointer data)
{
	printf("show_config_win\n");
    print_sys_config_s *sys_config = (print_sys_config_s *)data;
    gtk_widget_show_all(sys_config->config_win);
    start_soft_keyboard();
//    gtk_widget_grab_focus (GTK_WIDGET (sys_config->config_entry_url));
}

void show_config_sound_win(GtkWidget* widget, gpointer data)
{

	printf("show_config_sound_win\n");
    print_sys_config_s *sys_config = (print_sys_config_s *)data;
	FILE *fp = NULL;
    if(0 == sound_opening_flag) {
	   fp = popen("gnome-terminal --geometry=0x0+0+0 --hide-menubar -x gnome-control-center sound", "r");
       sound_opening_flag = 1;
    } else {
       fp = popen("sudo killall gnome-control-center sound", "r");
       sound_opening_flag = 0;
    }
	pclose(fp);

}

void create_config_win(print_sys_config_s *sys_config,GtkBuilder *builder)
{
	    printf("create_config_win\n");

    	sys_config->config_win= GTK_WIDGET(gtk_builder_get_object (builder, "config_win"));
    	sys_config->config_entry_mac = GTK_WIDGET(gtk_builder_get_object (builder, "config_mac_entry"));
//        sys_config->config_entry_eth0_mac = GTK_WIDGET(gtk_builder_get_object (builder, "config_eth0_mac_entry"));
    	sys_config->config_entry_url = GTK_WIDGET(gtk_builder_get_object (builder, "config_url_entry"));
    	sys_config->config_save_button = GTK_WIDGET(gtk_builder_get_object (builder, "config_button"));

        gtk_entry_set_text(GTK_EDITABLE(sys_config->config_entry_mac),sys_config->sys_config.eth0_mac_addr);
//        gtk_entry_set_text(GTK_EDITABLE(sys_config->config_entry_eth0_mac),sys_config->sys_config.eth0_mac_addr);
        gtk_entry_set_text(GTK_EDITABLE(sys_config->config_entry_url),sys_config->sys_config.heart_beat_url);

        gtk_widget_set_can_focus( sys_config->config_entry_url, TRUE);
          
        g_signal_connect(G_OBJECT(sys_config->config_save_button), "clicked",G_CALLBACK(save_config_win), sys_config);//set keyboard
        printf("create_config_win out[%s][%s]\n",sys_config->sys_config.mac_addr,sys_config->sys_config.heart_beat_url);
}

void show_message_win(print_sys_config_s *sys_config, char* text)
{
	printf("show_config_win\n");
    if(NULL != sys_config && NULL != text)  {
        update_text(sys_config->message_win_label,text,20000);
        gtk_widget_show_all(sys_config->message_win);
    }
}


void hide_message_win(print_sys_config_s *sys_config)
{
	printf("show_config_win\n");
    if(NULL != sys_config)
    gtk_widget_hide(sys_config->message_win);
}

void create_message_win(print_sys_config_s *sys_config,GtkBuilder *builder)
{
	    printf("create_message_win\n");

    	sys_config->message_win= GTK_WIDGET(gtk_builder_get_object (builder, "message_win"));
    	sys_config->message_win_label= GTK_WIDGET(gtk_builder_get_object (builder, "message_win_label"));
        gtk_window_move(sys_config->message_win,100,sys_config->screen_h-50);

}

int mp3_read_filelist(print_sys_config_s *sys_config)
{
    DIR *dir;
    struct dirent *ptr;
    char base[1000];
    char file[1024];
	int i = 0;

	memset(base,'\0',sizeof(base));
	getcwd(base, 999);

	strcat(base,MP3_PATH);
	
    if ((dir=opendir(base)) == NULL)
    {
        SYS_LOG("Open dir error...");
        exit(1);
    }
	FILE* fp = NULL;
	fp = fopen(sys_config->sys_config.mp3_playlist, "w+");
	fprintf(fp,"%s","<GLOBAL PLAYMODE=\"allgroups\">\n");
	fprintf(fp,"%s","<GROUP NAME=\"Default\">\n");
	fprintf(fp,"%s","<\/GROUP>\n");
	printf("dir is [%s]\n",base);
    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir
            continue;
        else if(ptr->d_type == 8) {    ///file
			printf("ptr->d_name is [%s]\n",ptr->d_name);
			if(g_strrstr(ptr->d_name,".mp3")) {//store
				memset(file,0,1024);
				sprintf(file,"%s/%s",base,ptr->d_name);
				fprintf(fp,"%s\n",file);
				
			}
		}
        else if(ptr->d_type == 10)    ///link file
            SYS_LOG("d_name:%s/%s\n",base,ptr->d_name);
     }
     closedir(dir);
     fclose(fp);
     return 1;
}
